// 请求返回状态码处理

import Vue from 'vue'
import router from '@/router/index'
import noticeModal from '@/components/ivew_components/notice_modal/index.vue'
import { deepCopy } from '@/utils/tools'
const _vue:any = new Vue();


/*
** 错误信息转换接口
** params: 
    source: 后台返回错误信息,
    transition: 转换后信息,
    config: 信息提示组件配置, 参考 iview文档 [Notice]
*/
interface errMsgTransition {

    source:string
    transition:string
    config?: any
}


let backErrMassage:errMsgTransition[] = [

    {
        source: '查找用户异常，，record not found',
        transition: '账户或密码错误',
        config: {
            duration: 10,
            type: 'warning',
        }
    },

    {
        source: '未找到车型数据',
        transition: '未找到相关车型, 如果车型数据输入无误, 可尝试再次查询或手动补全车型数据',
        config: {
            duration: 10,
            type: 'warning',
        }
    },
    
]

/*
** 转换后台错误提示
** params
    [conf]: 错误转换配置
    [defaultSet]: 默认错误配置
*/

class backErrTransition{

    sourceMsg:string[] = []
    conf:errMsgTransition[] = []
    sourceMsgSet
    

    // 默认错误提示
    defaultItem:errMsgTransition = {

        source: '',
        transition: '',
        config: {
            duration: 0,
            type: 'error',
        }

    }
    

    public constructor(conf: errMsgTransition[], defaultSet?:errMsgTransition){

        let _self = this;

        conf.forEach(item =>{
            _self.sourceMsg.push(item.source);
        })  
        
        _self.sourceMsgSet = new Set(_self.sourceMsg);
        _self.conf = conf;
        
        _self.defaultItem = defaultSet || _self.defaultItem;
        
    }


    transitionErrMsg (msg){

        let _self = this;

        // 查找对应数据
        let index = this.sourceMsg.findIndex((item) =>{
            return item === msg
        })

        let defaultItem = Object.assign(deepCopy(_self.defaultItem), {transition: msg})

        return index < 0 ? defaultItem : _self.conf[index]
    }

    
}


let trs = new backErrTransition(backErrMassage)


/*
** params
     s[number]: 请求状态码, s400
     e[number]: 请求成功,信息状态码, e400 
*/

const status = {
    
    s400: () =>{

        _vue.$Notice.error({
            title: '网络中断',
        })
        
    }, 
    
    e400: () =>{
        _vue.$Notice.error({
            title: '参数错误'
        })
    },
    
    e500: (resData) =>{


        let data = trs.transitionErrMsg(resData.message);

        _vue.$Notice[data.config.type]({
            
            title: '错误',
            desc: data.transition,
            ...data.config
            
        })
        
    },
    
    e501: () =>{
        _vue.$Notice.error({
            title: 'appkey失效'
        })
    },

    e502: () =>{
        _vue.$Notice.warning({
            title: 'ukey失效',
            desc: '登录时效过期, 请重新登录'
        })

        router.push('/sign-in')
    },
    
    e503: () =>{
        _vue.$Notice.error({
            title: 'ua失效'
        })
    },
    
    e504: () =>{
        _vue.$Notice.error({
            title: '权限不足'
        })
    },
    
    e200: (resData) =>{
        

    }
}


function filterCode (config:any):any{

    if(config.status !== 200){
        
        // 服务器错误
        let code = 's' + config.status;
        let preFn = status[code];
        if(preFn){preFn(config)}
        
    }else{

        // 信息请求错误
        let resData = config.data;
        let msgCode = 'e' + resData.statusCode;
        let preFn = status[msgCode];
        if(preFn)(preFn(resData));
    }
    
    return config
}







export default filterCode;